<?php

namespace app\api\controller;
use app\api\validate\PickValidate;
use app\api\validate\StoreValidate;
use app\common\model\Pick;
use app\common\model\Store;
use app\common\util\MyException;
use hg\apidoc\annotation as Apidoc;
use think\exception\ValidateException;

/**
 * @Apidoc\Title("商户")
 */
class StoreApi extends Base
{
    /**
     * @Apidoc\Title("开店申请")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/store_sq")
     * @Apidoc\Param("step",type="int",require=true,default="1",desc="当前步数")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("contacts_name",type="string",require=true,default="",desc="联系人")
     * @Apidoc\Param("contacts_mobile",type="string",require=true,default="",desc="联系电话")
     * @Apidoc\Param("store_name",type="string",require=true,default="",desc="店铺名称")
     * @Apidoc\Param("province",type="string",require=true,default="",desc="省份")
     * @Apidoc\Param("city",type="string",require=true,default="",desc="城市")
     * @Apidoc\Param("district",type="string",require=true,default="",desc="区/县")
     * @Apidoc\Param("store_address",type="string",require=true,default="",desc="详细地址")
     * @Apidoc\Param("longitude",type="string",require=true,default="",desc="经度")
     * @Apidoc\Param("latitude",type="string",require=true,default="",desc="纬度")
     * @Apidoc\Param("store_person_cert",type="string",require=true,default="",desc="身份证正面")
     * @Apidoc\Param("store_person_cert1",type="string",require=true,default="",desc="身份证反面")
     * @Apidoc\Param("store_logo",type="string",require=true,default="",desc="店铺门头")
     * @Apidoc\Param("business_licence_cert",type="string",require=true,default="",desc="营业执照")
     */
    public function store_apply(){
        $step=input('step');
        if (empty($step) || !in_array($step,[1,2])){
            throw new MyException('提交参数有误');
        }
        $store_model=new Store();
        $store=$store_model->where('user_id',$this->user_id)->find();
        if ($store){
            if ($store['status']==-1){//待审核
                $edit=0;//待审核时不能编辑
                $edit_text="待审核";
            }elseif ($store['status']==1){//已申请通过
                $edit=1;//已开通店铺
                $edit_text="已开通";
            }elseif ($store['status']==2){
                $edit=-1;//审核失败可编辑
                $edit_text="更新提交";
            }elseif ($store['status']==3){
                $edit=2;//店铺被关闭
                $edit_text="已关闭";
            }
        }else{
            $edit=-100;//未申请过可申请店铺
            $edit_text="提交申请";
        }
        if ($step==1){
            $return_data['edit']=$edit;
            $return_data['edit_text']=$edit_text;
            $return_data['store']=$store;
            throw new MyException('获取成功',10,$return_data);
        }else{
            if (!in_array($edit,[-1,-100])){
                throw new MyException('当前状态不可编辑');
            }
            $data = input('post.');
            if ($store){
                $data['store_id']=$store['store_id'];
            }
            try {
                validate(StoreValidate::class)->check($data);
            } catch (ValidateException $e) {
                $error = $e->getError();
                throw new MyException($error);
            }
            $data['store_person_cert']=str_replace(SITE_URL,'',$data['store_person_cert']);
            $data['store_person_cert1']=str_replace(SITE_URL,'',$data['store_person_cert1']);
            $data['store_logo']=str_replace(SITE_URL,'',$data['store_logo']);
            $data['business_licence_cert']=str_replace(SITE_URL,'',$data['business_licence_cert']);
            if ($edit==-100){
                $res=$store_model->save($data);
                if ($res){
                    throw new MyException('提交成功',200);
                }else{
                    throw new MyException('提交失败');
                }
            }elseif ($edit==-1){
                $data['status']=-1;
                $data['refuse_time']=null;
                $res=$store->save($data);
                if ($res!==false){
                    throw new MyException('提交成功',200);
                }else{
                    throw new MyException('提交失败或没有任何修改');
                }
            }
        }
    }
    /**
     * @Apidoc\Title("商家详情")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/store_xq")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("store_id",type="int",require=true,default="",desc="商家id")
     */
    public function store_detail()
    {
        $store_id = input('store_id/d', 0);
        if (empty($store_id)){
            throw new MyException('提交参数有误');
        }
        $store = Store::where('store_id',$store_id)
            ->field("store_name,contacts_name,contacts_mobile,province,city,district,store_address,store_logo,business_licence_cert,check_time,status,longitude,latitude")
            ->find();
        if (empty($store)) {
            throw new MyException('商家不存在');
        }
        if ($store['status']!=1){
            throw new MyException('商家不存在');
        }
        $store['check_time']=date('Y-m-d',$store->getData('check_time'));
        $data['store'] = $store;
        throw new MyException('获取成功', 200, $data);
    }
    /**
     * @Apidoc\Title("自提点申请")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/ztd_sq")
     * @Apidoc\Param("step",type="int",require=true,default="1",desc="当前步数")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("contacts_name",type="string",require=true,default="",desc="联系人")
     * @Apidoc\Param("contacts_mobile",type="string",require=true,default="",desc="联系电话")
     * @Apidoc\Param("store_name",type="string",require=true,default="",desc="店铺名称")
     * @Apidoc\Param("province",type="string",require=true,default="",desc="省份")
     * @Apidoc\Param("city",type="string",require=true,default="",desc="城市")
     * @Apidoc\Param("district",type="string",require=true,default="",desc="区/县")
     * @Apidoc\Param("store_address",type="string",require=true,default="",desc="详细地址")
     * @Apidoc\Param("longitude",type="string",require=true,default="",desc="经度")
     * @Apidoc\Param("latitude",type="string",require=true,default="",desc="纬度")
     * @Apidoc\Param("store_logo",type="string",require=true,default="",desc="店铺门头")
     */
    public function pick_apply(){
        $step=input('step');
        if (empty($step) || !in_array($step,[1,2])){
            throw new MyException('提交参数有误');
        }
        $store_model=new Pick();
        $store=$store_model->where('user_id',$this->user_id)->find();
        if ($store){
            if ($store['status']==-1){//待审核
                $edit=0;//待审核时不能编辑
                $edit_text="待审核";
            }elseif ($store['status']==1){//已申请通过
                $edit=1;//已开通
                $edit_text="已开通";
            }elseif ($store['status']==2){
                $edit=-1;//审核失败可编辑
                $edit_text="更新提交";
            }elseif ($store['status']==3){
                $edit=2;//被关闭
                $edit_text="已关闭";
            }
        }else{
            $edit=-100;//未申请过可申请店铺
            $edit_text="提交申请";
        }
        if ($step==1){
            $return_data['edit']=$edit;
            $return_data['edit_text']=$edit_text;
            $return_data['store']=$store;
            throw new MyException('获取成功',10,$return_data);
        }else{
            if (!in_array($edit,[-1,-100])){
                throw new MyException('当前状态不可编辑');
            }
            $data = input('post.');
            if ($store){
                $data['store_id']=$store['store_id'];
            }
            try {
                validate(PickValidate::class)->check($data);
            } catch (ValidateException $e) {
                $error = $e->getError();
                throw new MyException($error);
            }
            $data['store_logo']=str_replace(SITE_URL,'',$data['store_logo']);
            if ($edit==-100){
                $res=$store_model->save($data);
                if ($res){
                    throw new MyException('提交成功',200);
                }else{
                    throw new MyException('提交失败');
                }
            }elseif ($edit==-1){
                $data['status']=-1;
                $data['refuse_time']=null;
                $res=$store->save($data);
                if ($res!==false){
                    throw new MyException('提交成功',200);
                }else{
                    throw new MyException('提交失败或没有任何修改');
                }
            }
        }
    }
    /**
     * @Apidoc\Title("自提点列表")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/ztd_list")
     * @Apidoc\Param(ref="pagingParam")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("longitude",type="string",require=true,default="",desc="经度")
     * @Apidoc\Param("latitude",type="string",require=true,default="",desc="纬度")
     */
    public function get_pick_list(){
        $pi = input('pageIndex',1);
        $ps = input('pageSize',20);
        //115.503008,35.230285 交通未来城
        $longitude=input('longitude')?:'115.503008';
        $latitude=input('latitude')?:'35.230285';
        $field="id,user_id,store_name,store_logo,province,city,district,store_address,district,longitude,latitude,";
        $field.="ACOS(SIN(($latitude*3.1415)/180)*SIN((latitude*3.1415)/180)+COS(($latitude*3.1415)/180)*COS((latitude*3.1415)/180)*COS(($longitude*3.1415)/180-(longitude*3.1415)/180))*6380 as dis";
        $where="status=1";
        $list = Pick::where($where)
            ->order('store_sort asc,id desc')
            ->page($pi, $ps)
            ->field($field)
            ->select()->each(function ($item){
                $item['all_address']=$item['all_address'];
                return $item;
            });
        $data['list'] = $list;
        throw new MyException('获取成功',200,$data);
    }
    /**
     * @Apidoc\Title("自提点详情")
     * @Apidoc\Method("POST")
     * @Apidoc\url("api/ztd_xq")
     * @Apidoc\Param("user_id",type="int",require=true,default="",desc="用户id")
     * @Apidoc\Param("id",type="int",require=true,default="",desc="自提点id")
     */
    public function pick_detail()
    {
        $id = input('id/d', 0);
        if (empty($id)){
            throw new MyException('提交参数有误');
        }
        $store = Pick::where('id',$id)
            ->field("store_name,contacts_name,contacts_mobile,province,city,district,store_address,store_logo,check_time,status,latitude,longitude")
            ->find();
        if (empty($store)) {
            throw new MyException('自提点不存在');
        }
        if ($store['status']!=1){
            throw new MyException('自提点不存在');
        }
        $store['check_time']=date('Y-m-d',$store->getData('check_time'));
        $data['store'] = $store;
        throw new MyException('获取成功', 200, $data);
    }
}
